package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo12 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username  =  scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nick = scanner.nextLine();
        try(  Connection conn = DBUtils.getConn()) {
          String sql ="insert into user values(null ,?,?,?)";
            PreparedStatement ps =conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,nick);
          String sql2 ="select username from user ";
          PreparedStatement ps2 =conn.prepareStatement(sql2);
          ResultSet rs = ps2.executeQuery();
          while (rs.next()){
              String name = rs.getString("username");
              if(name.equals(username)){
                  System.out.println("用户名已存在");
                  return;
              }
          }
            ps.executeUpdate();
            System.out.println("注册成功");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
